package com.topmap.service.impl;

import com.topmap.dao.FeiyiPeopleDOMapper;
import com.topmap.dataobject.FeiyiPeopleDO;
import com.topmap.service.FeiyiPeopleService;
import com.topmap.service.model.FeiyiPeopleModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: MaHuadong
 * @Date: 2019/7/3 11:13
 * @Version 1.0
 */
@Service
public class FeiyiPeopleServiceImpl implements FeiyiPeopleService{

    @Autowired
    private FeiyiPeopleDOMapper feiyiPeopleDOMapper;

    @Autowired
    private FeiyiPeopleService feiyiPeopleService;


    //通过非遗传承人名字查询非遗的具体实现方法
    @Override
    public FeiyiPeopleModel getFeiyiPeopleByPeople(String people) {
        FeiyiPeopleDO feiyiPeopleDO=feiyiPeopleDOMapper.selectByFeiyiPeople(people);

        if (feiyiPeopleDO==null){
            return null;
        }

        //将FeiyiDO转换为FeiyiModel
        FeiyiPeopleModel feiyiPeopleModel=convertModelFromDataObject(feiyiPeopleDO);

        return feiyiPeopleModel;
    }

    //查询所有非遗信息
    @Override
    public List<FeiyiPeopleModel> listFeiyiPeople() {
        //使用mapper的listProduct方法查询所有的商品dataobject
        List<FeiyiPeopleDO> feiyiPeopleDOList=feiyiPeopleDOMapper.listFeiyiPeople();

        //使用java8的stream api把 feiyiDOList中的每一个都转化出来
        List<FeiyiPeopleModel> feiyiPeopleModelList=feiyiPeopleDOList.stream().map(feiyiPeopleDO -> {
            //map成feiyiModel
            FeiyiPeopleModel feiyiPeopleModel=this.convertModelFromDataObject(feiyiPeopleDO);
            return feiyiPeopleModel;
        }).collect(Collectors.toList());
        //返回出去productModelList
        return feiyiPeopleModelList;
    }

    private FeiyiPeopleModel convertModelFromDataObject(FeiyiPeopleDO feiyiPeopleDO) {

        FeiyiPeopleModel feiyiPeopleModel=new FeiyiPeopleModel();


        //copy FeiyiDO
        BeanUtils.copyProperties(feiyiPeopleDO,feiyiPeopleModel);

        return feiyiPeopleModel;
    }

}
